package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/*
*  定时任务类
* */
@Component
public class ScheduleTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    /*
    *   定时检查租约的过期状态
    *     确定租约过期了:
    *     1.status租约状态是(2,5)
    *     2.lease_end_date租约到期时间 < 当前时间
    * */
//    @Scheduled(cron = "* * * * * *")
    public void checkLeaseStatus(){
        leaseAgreementService.update(new LambdaUpdateWrapper<LeaseAgreement>()
                .le(LeaseAgreement::getLeaseEndDate,new Date())
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING)
                .set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED)
        );
    }
}
